package com.supeng.PMdepot.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nineoldandroids.view.ViewHelper;
import com.supeng.PMdepot.R;
import com.supeng.PMdepot.base.BaseActivity;
import com.supeng.PMdepot.common.Config;
import com.supeng.PMdepot.entity.ServiceResult;
import com.supeng.PMdepot.entity.UpdateInfo;
import com.supeng.PMdepot.net.Api;
import com.supeng.PMdepot.net.NetUtils;
import com.supeng.PMdepot.util.Utils;
import com.supeng.PMdepot.widget.dialog.CUpdateDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.title_bar)
    RelativeLayout titleBar;
    @BindView(R.id.fl_ruku)
    FrameLayout flRuku;
    @BindView(R.id.fl_chuku)
    FrameLayout flChuku;
    @BindView(R.id.fl_chuku_other)
    FrameLayout flChukuOther;
    @BindView(R.id.fl_yiku)
    FrameLayout flYiku;
    @BindView(R.id.fl_pandian)
    FrameLayout flPandian;
    @BindView(R.id.fl_search)
    FrameLayout flSearch;
    @BindView(R.id.fl_chaixiang)
    FrameLayout flChaixiang;
    @BindView(R.id.root_layout)
    LinearLayout rootLayout;
    @BindView(R.id.tv_login_name)
    TextView tvLoginName;
    private ImageView iv_menu;
    private DrawerLayout mDrawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //弹出键盘滚动问题
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_main);
        unbinder = ButterKnife.bind(this);
        initView();
        initData();
         getUpdateAutoCheck();
    }

    @OnClick({R.id.fl_ruku, R.id.fl_chuku, R.id.fl_chuku_other, R.id.fl_yiku, R.id.fl_pandian, R.id.fl_search,R.id.fl_chaixiang,R.id.fl_waifachaixiang,R.id.fl_zaizhipinruku,R.id.fl_zaizhipinSearch})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_ruku:
                startActivity(new Intent(getActivity(), RuKuActivity.class));
                break;
            case R.id.fl_chuku:
                startActivity(new Intent(getActivity(), ChukuActivity.class));
                break;
            case R.id.fl_chuku_other:
                startActivity(new Intent(getActivity(), ChukuOtherActivity.class));
                break;
            case R.id.fl_chaixiang:
                startActivity(new Intent(getActivity(),ChaXiangActivity.class));
                break;
            case R.id.fl_yiku:
               // ToastUtils.show("功能占未开放...");

                startActivity(new Intent(getActivity(), YiKuActivity.class));
                break;
            case R.id.fl_pandian:
                //ToastUtils.show("功能占未开放...");

                startActivity(new Intent(getActivity(), PanDianActivity.class));
                break;
            case R.id.fl_search:
                //ToastUtils.show("功能占未开放...");

                startActivity(new Intent(getActivity(), SearchActivity.class));
                break;
            case R.id.fl_waifachaixiang:
                startActivity(new Intent(getActivity(), WFChaXiangActivity.class));
                break;
            case R.id.fl_zaizhipinruku:
                startActivity(new Intent(getActivity(), ZaiZhiRuKuActivity.class));
                break;
            case R.id.fl_zaizhipinSearch:
                startActivity(new Intent(getActivity(), ZaiZhiPinSearchActivity.class));
                break;
        }
    }

    private void initData() {


    }

    private void initView() {

        iv_menu = (ImageView) findViewById(R.id.iv_menu);
        iv_menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openLeftDrawerLayout();
            }
        });
        tvLoginName.setText("用户("+ Config.getUserInfo().getUsername()+")");
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        // 去除阴影
        mDrawerLayout.setScrimColor(Color.TRANSPARENT);
        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, Gravity.RIGHT);
        mDrawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerStateChanged(int newState) {
            }

            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                View mContent = mDrawerLayout.getChildAt(0);
                View mMenu = drawerView;
                float scale = 1 - slideOffset;
                float rightScale = 0.8f + scale * 0.2f;

                if (drawerView.getTag().equals("LEFT")) {
                    float leftScale = 1 - 0.3f * scale;
                    ViewHelper.setScaleX(mMenu, leftScale);
                    ViewHelper.setScaleY(mMenu, leftScale);
                    ViewHelper.setAlpha(mMenu, 0.6f + 0.4f * (1 - scale));
                    ViewHelper.setTranslationX(mContent,
                            mMenu.getMeasuredWidth() * (1 - scale));
                    ViewHelper.setPivotX(mContent, 0);
                    ViewHelper.setPivotY(mContent,
                            mContent.getMeasuredHeight() / 2);
                    mContent.invalidate();
                    ViewHelper.setScaleX(mContent, rightScale);
                    ViewHelper.setScaleY(mContent, rightScale);
                } else {
                    ViewHelper.setTranslationX(mContent,
                            -mMenu.getMeasuredWidth() * slideOffset);
                    ViewHelper.setPivotX(mContent, mContent.getMeasuredWidth());
                    ViewHelper.setPivotY(mContent,
                            mContent.getMeasuredHeight() / 2);
                    mContent.invalidate();
                    ViewHelper.setScaleX(mContent, rightScale);
                    ViewHelper.setScaleY(mContent, rightScale);
                }
            }

            @Override
            public void onDrawerOpened(View drawerView) {
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                mDrawerLayout.setDrawerLockMode(
                        DrawerLayout.LOCK_MODE_LOCKED_CLOSED, Gravity.RIGHT);
            }
        });
    }

    /**
     * 打开侧边栏
     */
    public void openLeftDrawerLayout() {
        mDrawerLayout.openDrawer(Gravity.LEFT);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
                mDrawerLayout.closeDrawer(Gravity.LEFT);
            } else {
                moveTaskToBack(true);
            }
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }


    /**
     * 获取更新
     */
    public void getUpdateAutoCheck() {
        Api.checkUpdate(getActivity(), new NetUtils.NetCallBack<ServiceResult>() {
            @Override
            public void success(ServiceResult rspData) {
                UpdateInfo update = (UpdateInfo) rspData;
                //  if (update.getRecode()==1) {
                int versionCode = update.getVersionCode();
                int currVer = Utils.getClientVersionCode(getApplication());
                if (versionCode > currVer) {
                    showUpdateDialog(update);
                }
                //  }
            }

            @Override
            public void failed(String msg) {
                Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onUsedCache(String msg) {

            }
        }, UpdateInfo.class);
    }

    /**
     * 显示软件下载对话框，在线更新
     */
    private void showUpdateDialog(UpdateInfo update) {
        CUpdateDialog dialog = new CUpdateDialog(this, update);
        dialog.show();
    }

}
